package giermala.dominik.fotka.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "users")
public class User implements Serializable, UserDetails {

	private static final long serialVersionUID = 8019494560156186656L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(unique=true)
	private String username;
	private String password;
	private boolean enabled = true;
	private boolean accountNonExpired = true;
	private boolean credentialsNonExpired = true;
	private boolean accountNonLocked = true;
	
	@OneToOne(mappedBy="fotkaUser", cascade={CascadeType.ALL})
	private Role role;
	
	@OneToMany(mappedBy="fotkaUser", cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
	private final List<Foto> fotos = new LinkedList<Foto>();
	
	@OneToMany(mappedBy="fotkaUser", cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
	private final List<Note> notes = new LinkedList<Note>();
	
	@OneToMany(mappedBy="fotkaUser", cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
	private final List<Comment> comments = new LinkedList<Comment>();
	
	private String firstName;
	private String lastName;


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	public void setAccountNonExpired(Boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	public void setAccountNonLocked(Boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public List<Foto> getFotos() {
		return fotos;
	}
	
	public List<Note> getNotes() {
		return notes;
	}

	public List<Comment> getComments() {
		return comments;
	}
	
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<String> roles = getRoles(role.getRole());
		List<GrantedAuthority> authList = getGrantedAuthorities(roles);
		return authList;
	}

	public List<String> getRoles(Integer role) {
		List<String> roles = new ArrayList<String>();

		if (role.intValue() == 1) {
			roles.add("ROLE_USER");
			roles.add("ROLE_ADMIN");

		} else if (role.intValue() == 2) {
			roles.add("ROLE_USER");
		}

		return roles;
	}

	public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		for (String role : roles) {
			authorities.add(new SimpleGrantedAuthority(role));
		}
		return authorities;
	}
	
	@Override
	public String toString() {
		return "id: " + Long.toString(id) + ", username: " + username
				+ ", password: " + password + ", enabled:"
				+ Boolean.toString(enabled) + ", firstName:" + firstName
				+ ", lastName: " + lastName;
	}
}
